/*
 Copyright (c) 2010, Advanced Micro Devices
 All rights reserved.
 
 Redistribution and use in source and binary forms, with or without modification, are permitted 
 provided that the following conditions are met:
 
 Redistributions of source code must retain the above copyright notice, this list of conditions 
 and the following disclaimer.
 
 Redistributions in binary form must reproduce the above copyright notice, this list of 
 conditions and the following disclaimer in the documentation and/or other materials provided 
 with the distribution.
 
 Neither the name of Advanced Micro Devices nor the names of its contributors may be used to endorse 
 or promote products derived from this software without specific prior written permission.
 
 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR 
 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY 
 AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR 
 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 
 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 
 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 
 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 
 OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
 POSSIBILITY OF SUCH DAMAGE.
 */

#ifndef __EDITORWINDOW_H__
#define __EDITORWINDOW_H__

#include <QMainWindow>
#include <QPair>
#include <QThread>
#include <QTreeWidget>
#include <QHBoxLayout>
#include <QMap>

class QAction;
class QGraphicsItem;
class QGraphicsScene;
class QGraphicsView;

#include "EditorView.h"

#include "link.h"
#include "node.h"

#include "Uniform.h"
#include "Equation.h"
#include "buffer.h"
#include "Kernel.h"
#include "Stage.h"
#include "graph.h"

class EditorWindow : public QMainWindow
{
    Q_OBJECT

public:
    EditorWindow();
	void createTree();
	void coalesceGraphs( Graph *, Graph * );
	void initializeOneNew( QString lhsName, QString dataType, QString rhs);
	void initializeNew();

private slots:
#if 0
    void addNode();
#endif
    void addLink();
	void addBuffer();
	void addStage();
	void addKernel();
	void addUniform();
	void addEquation();
	void addGraph( Node *, Node * );
    void del();
    void cut();
    void copy();
    void paste();
    void bringToFront();
    void sendToBack();
    void properties();
    void updateActions();
	void allocateGraphicsSurface();
	void saveAs();
	void save();
	void open();
	void new_();
	void saveObjects();
	void restoreObjects();
	void clearObjects();
	void readXmlObjects( QXmlStreamReader & );
	void buildProgram();
	void restoreGraphs();
	void displayPropertiesFromTree( QTreeWidgetItem * item, int column );
	void selectObjectFromTree( QTreeWidgetItem * item, int column );


private:
    typedef QPair<Node *, Node *> NodePair;

    void createActions();
    void createMenus();
    void createToolBars();
	void populateTree();
    void setZValue(int z);
    void setupNode(Node *node);
    Node *selectedNode() const;
    Link *selectedLink() const;
    NodePair selectedNodePair() const;
	void restoreLinks();
	Graph * findGraph( Node * node );
	void deleteGraphEdge( Link * link );
	QString newGraphName();
    QMenu * fileMenu;
    QMenu * editMenu;
	QMenu * insertMenu;
	QMenu * buildMenu;
    QToolBar *editToolBar;
    QAction *exitAction;
	QAction * layoutAction;
    QAction *addNodeAction;
    QAction *addLinkAction;
    QAction *deleteAction;
    QAction *cutAction;
    QAction *copyAction;
    QAction *pasteAction;
    QAction *bringToFrontAction;
    QAction *sendToBackAction;
    QAction *propertiesAction;
	QAction * buildAction;

	QAction * newAction;
	QAction * openAction;
	QAction * importAction;
	QAction * closeAction;
	QAction * saveAction;
	QAction * saveAsAction;

	QAction * bufferAction;
	QAction * stageAction;
	QAction * kernelAction;
	QAction * uniformAction;
	QAction * equationAction;
	
    //QGraphicsScene *scene;
	QGraphicsScene * mScene;
    //QGraphicsView *view;
	EditorView * mView;
	
	QTreeWidget * tree;
	
	Graphs mControlGraphs;
	Graphs mDataFlowGraphs;
	Buffers mBuffers;
	Stages mStages;
	Kernels mKernels;
	Uniforms mUniforms;
	Equations mEquations;

	Graphs mControlGraphs_;
	Buffers mBuffers_;
	Stages mStages_;
	Kernels mKernels_;
	Uniforms mUniforms_;
	Equations mEquations_;
	
	QDockWidget * mDockWidget;

    int minZ;
    int maxZ;
    int seqNumber;
	
	int width;
	int height;

	QString mCurrentFileName;
	
public:
	Graphs & controlGraphs() { return mControlGraphs; }
	Buffers & buffers() { return mBuffers; }
	Stages & stages() { return mStages; }
	Kernels & kernels() { return mKernels; }
	Uniforms & uniforms() { return mUniforms; }
	Equations & equations() { return mEquations; }

	QString currentFileName() const{ return mCurrentFileName; }
	void currentFileNameIs( QString currentFileName ){ mCurrentFileName = currentFileName; }
	QString currentBaseFileName() const;

	QGraphicsScene * scene() { return mScene; }
	QGraphicsView * view(){ return mView; }
};

// this class exists so the GUI can run in a separate thread
class EditorWindowContainer : public QThread
{
public:
	EditorWindowContainer()
	{
		view.initializeNew();
		view.show();
	}
	EditorWindow view;
	
protected:
	void run()
	{
	}
};

#endif
